fix: implementa atualização automática de contatos @lid#1845
fix: implementa atualização automática de contatos @lid#1845ricaelchiquetti wants to merge 7 commits intoEvolutionAPI:developfrom ricaelchiquetti:main
Conversation
Reviewer's GuideThis PR introduces a full lifecycle for resolving and cleaning up WhatsApp contacts created with a Sequence diagram for periodic @lid contact cleanup and resolutionsequenceDiagram
participant Scheduler
participant BaileysStartupService
participant Database
participant RedisCache
participant WhatsAppClient
participant LocalAuthState
Scheduler->>BaileysStartupService: Trigger cleanupOrphanedLidContacts()
BaileysStartupService->>Database: Find contacts with @lid
Database-->>BaileysStartupService: Return @lid contacts
loop For each @lid contact
BaileysStartupService->>WhatsAppClient: Resolve real JID via onWhatsApp()
WhatsAppClient-->>BaileysStartupService: Return real JID or not found
alt Real JID found
BaileysStartupService->>Database: Update contact to real JID
BaileysStartupService->>RedisCache: Update cache key to real JID
BaileysStartupService->>LocalAuthState: Update local contact to real JID
else Not found
BaileysStartupService->>Database: Remove orphaned contact
BaileysStartupService->>RedisCache: Remove orphaned cache key
BaileysStartupService->>LocalAuthState: Remove orphaned local contact
end
end
Class diagram for BaileysStartupService contact and message update logicclassDiagram
class BaileysStartupService {
+startLidCleanupScheduler()
+cleanupOrphanedLidContacts()
+updateContactFromLid(lidJid, realJid)
+eventHandler()
+formatUpdateMessage(data)
}
class PrismaRepository {
+contact
+message
+messageUpdate
}
class Cache {
+hGet()
+hSet()
+hDelete()
+keys()
}
class WhatsAppClient {
+onWhatsApp(jid)
+sendMessage(jid, options)
}
BaileysStartupService --> PrismaRepository : uses
BaileysStartupService --> Cache : uses
BaileysStartupService --> WhatsAppClient : uses
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:673` </location>
<code_context>
this.eventHandler();
+ this.startLidCleanupScheduler();
+
this.client.ws.on('CB:call', (packet) => {
</code_context>
<issue_to_address>
Consider making the lid cleanup scheduler opt-in or configurable.
Some deployments may require tighter control over resource usage or side effects, so providing a configuration option to enable or disable the scheduler would improve flexibility.
Suggested implementation:
```typescript
this.eventHandler();
if (this.config?.enableLidCleanupScheduler) {
this.startLidCleanupScheduler();
}
this.client.ws.on('CB:call', (packet) => {
```
1. Ensure that the class has access to a `config` object or property. If not, you will need to add it to the constructor or initialization logic.
2. Document the new configuration option (`enableLidCleanupScheduler`) in your configuration schema or documentation.
3. Update any relevant tests to cover both enabled and disabled scenarios.
</issue_to_address>
### Comment 2
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:4125` </location>
<code_context>
+ for (const key of keys) {
</code_context>
<issue_to_address>
Nested loop over keys may be redundant and impact performance.
The inner loop over 'keys' may cause unnecessary repeated work. Consider refactoring to avoid double iteration.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
|
||
| this.eventHandler(); | ||
|
|
||
| this.startLidCleanupScheduler(); |
There was a problem hiding this comment.
suggestion: Consider making the lid cleanup scheduler opt-in or configurable.
Some deployments may require tighter control over resource usage or side effects, so providing a configuration option to enable or disable the scheduler would improve flexibility.
Suggested implementation:
this.eventHandler();
if (this.config?.enableLidCleanupScheduler) {
this.startLidCleanupScheduler();
}
this.client.ws.on('CB:call', (packet) => {- Ensure that the class has access to a
configobject or property. If not, you will need to add it to the constructor or initialization logic. - Document the new configuration option (
enableLidCleanupScheduler) in your configuration schema or documentation. - Update any relevant tests to cover both enabled and disabled scenarios.
| const previousRemoteJid = received.key.remoteJid; | ||
| received.key.remoteJid = received.key.senderPn; | ||
| await this.updateContactFromLid(previousRemoteJid, received.key.remoteJid); | ||
| } |
There was a problem hiding this comment.
issue (performance): Nested loop over keys may be redundant and impact performance.
The inner loop over 'keys' may cause unnecessary repeated work. Consider refactoring to avoid double iteration.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Correção e implementação da lógica de resolução de contatos @lid
Descrição
Este PR implementa e corrige a lógica de resolução automática de contatos criados com JID
@lidtanto no banco de dados quanto no cache Redis.Agora, contatos órfãos com
@lidsão periodicamente verificados e, quando possível, atualizados para o JID real.A lógica cobre os seguintes cenários:
@lid, tenta resolver o JID real via Baileys e atualiza ou remove o contato conforme necessário.@lid, tenta resolver o JID real e atualiza o cache, removendo a chave antiga.Motivo
@lid, especialmente em dispositivos iPhone.Como funciona
cleanupOrphanedLidContactsexecuta periodicamente a limpeza e resolução dos contatos.updateContactFromLidfaz a atualização do contato e das mensagens associadas ao JID real.Testes
@lidsão resolvidos e atualizados corretamente.Summary by Sourcery
Implement automatic resolution and cleanup of orphaned @lid contacts by remapping them to real JIDs at runtime and scheduling periodic cleanup, while making message persistence conditional on configuration flags and enhancing observability through detailed logs
New Features:
Bug Fixes:
Enhancements: